package com.course.coursedesign2022.service;

import com.course.coursedesign2022.mapper.UserMapper;
import com.course.coursedesign2022.pojo.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class BfzNote {

    @Autowired
    private UserMapper userMapper;

    public UserInfo bfzNote(Integer id){
        //读取数据
        UserInfo userInfoById = userMapper.getUserInfoById(id);
        Date date = new Date();
        Integer growscore = userInfoById.getGrowscore();
        Integer scoretotal = userInfoById.getScoretotal();
        Date bfzFillDate = userInfoById.getBfzFillDate();
        //并发症上次填写时间为空
        if(bfzFillDate==null){
            System.out.println("今年第一次填写并发症记录，成长积分加1");
            growscore += 1;
            scoretotal += 1;
            bfzFillDate = date;
            userInfoById.setGrowscore(growscore);
            userInfoById.setScoretotal(scoretotal);
            userInfoById.setBfzFillDate(bfzFillDate);
            userMapper.updateUserInfo(userInfoById);
            return userInfoById;
        }
        int year = bfzFillDate.getYear();

        //判断上次填写并发症日期与本次是否同一年
        //同年，不计分
        if (year == date.getYear()){
            System.out.println("今年已经填写过并发症记录");
        }else{
        //新的一年，增加1成长积分，修改上次填写并发症日期
            System.out.println("今年第一次填写并发症记录，成长积分加1");
            growscore += 1;
            scoretotal += 1;
            bfzFillDate = date;
            userInfoById.setGrowscore(growscore);
            userInfoById.setScoretotal(scoretotal);
            userInfoById.setBfzFillDate(bfzFillDate);
        }
        System.out.println("成长积分为："+ userInfoById.getGrowscore());
        System.out.println("可交换积分为："+ userInfoById.getExchangescore());
        System.out.println("总积分为："+ userInfoById.getScoretotal());


        //写回数据库
        userMapper.updateUserInfo(userInfoById);
        return userInfoById;
    }
}
